﻿namespace MAdesantis
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;

    public class PersistAppData : SPPersistedObject
    {
        [Persisted]
        public Dictionary<string, string> Entries;

        public PersistAppData() { }
        public PersistAppData(string Name, SPPersistedObject Parent, Guid Id) : base(Name, Parent, Id) { }

        public const string DATA_TAG = "34694ce3-fb5b-49cb-b675-5a94b40df5c2";
        public const string SELECTED_INDEX = "SelectedIndex";
        public const string SELECTED_INDEX_ddlTiempo = "SelectedIndex_ddlTiempo";
        public const string Query = "Query";
        public const string URLSite = "URLSite";
        public const string DiasVencimiento = "DiasVencimiento";


        private static PersistAppData local;
        public static PersistAppData Local
        {
            get
            {
                {
                    SPAdministrationWebApplication caWebApp = SPAdministrationWebApplication.Local;
                    local = caWebApp.GetChild<PersistAppData>(DATA_TAG);

                    if (local == null)
                    {
                        Guid id = Guid.NewGuid();
                        SPPersistedObject parentWebApp = (SPPersistedObject)caWebApp;
                        local = new PersistAppData(DATA_TAG, parentWebApp, id);
                        local.Update();
                    }

                    if (local.Entries == null)
                    {
                        local.Entries = new Dictionary<string, string>();
                        local.Entries.Add(SELECTED_INDEX, "0");
                        local.Entries.Add(SELECTED_INDEX_ddlTiempo, "1");
                        local.Entries.Add(Query, "2");
                        local.Entries.Add(URLSite, "3");
                        local.Entries.Add(DiasVencimiento, "4");
                        local.Update();
                    }
                }
                return local;
            }
        }
    }
}
